package priv.dajie.tzzq.spider.dota.steam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.HttpGet;
import priv.dajie.tzzq.Base;
import priv.dajie.tzzq.spider.model.SpiderInfo;
import java.util.HashMap;
import java.util.Map;

public class SteamInfos {

    private static Base client = Base.getClientInstance();

    public static SpiderInfo getSteamInfos(){
        String html = client.open(new HttpGet("https://steamcommunity.com/market/appfilters/570"), true);
        JSONObject json = JSON.parseObject(html).getJSONObject("facets");
        Map<String, String> heroCN = getHeros(json);
        Map<String, String> rarityCN = getRarity(json);
        Map<String, String> typeCN = getType(json);
        return new SpiderInfo(heroCN, rarityCN, typeCN);
    }

    private static Map<String, String> getHeros(JSONObject json){
        Map<String, String> heroCN = new HashMap<>();
        JSONObject heros = json.getJSONObject("570_Hero").getJSONObject("tags");
        for (String key : heros.keySet()) {
            heroCN.put(key, heros.getJSONObject(key).getString("localized_name"));
        }
        heroCN.remove("DOTA_OtherType");
        return heroCN;
    }

    private static Map<String, String> getRarity(JSONObject json){
        Map<String, String> rarityCN = new HashMap<>();
        JSONObject raritys = json.getJSONObject("570_Rarity").getJSONObject("tags");
        for(String key : raritys.keySet()){
            rarityCN.put(key, raritys.getJSONObject(key).getString("localized_name"));
        }
        return rarityCN;
    }

    private static Map<String, String> getType(JSONObject json){
        Map<String, String> typeCN = new HashMap<>();
        JSONObject types = json.getJSONObject("570_Type").getJSONObject("tags");
        for(String key : types.keySet()){
            typeCN.put(key, types.getJSONObject(key).getString("localized_name"));
        }
        typeCN.remove("bundle");
        typeCN.remove("taunt");
        typeCN.remove("wearable");
        return typeCN;
    }

    public static void main(String[] args) {
        SpiderInfo spiderInfo = getSteamInfos();
        System.out.println(1);
    }
}
